package com.shqcjd.sinopecdemomobile.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.ui.activity.EquipmentAbnormalActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.DialogAbnormalAdapter;

import java.util.ArrayList;
import java.util.List;


public class AbnormalGroupDialog extends Dialog {
    private Context context;

    private OnBtnClickListener onBtnClickListener;
    private Button btn_submit;

    public interface OnBtnClickListener {
        void onBtnClick(View view, List<String> mList);
    }
    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }


    public AbnormalGroupDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }
    private RelativeLayout rl_dialog_abnormal_close;
    private LinearLayout ll_dialog_abnormal;
    private ImageView iv_dialog_abnormal_close;
    public EditText et_dialog_abnormal;
    private RecyclerView rv_dialog_abnormal;
    private DialogAbnormalAdapter dialogAbnormalAdapter;
    private List<String> mList = new ArrayList<>();
    private EquipmentAbnormalActivity mActivity;

    public AbnormalGroupDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        mActivity = (EquipmentAbnormalActivity) context;
    }


    protected AbnormalGroupDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_abnormal);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        rl_dialog_abnormal_close = findViewById(R.id.rl_dialog_abnormal_close);
        ll_dialog_abnormal = findViewById(R.id.ll_dialog_abnormal);
        iv_dialog_abnormal_close = findViewById(R.id.iv_dialog_abnormal_close);
        et_dialog_abnormal = findViewById(R.id.et_dialog_abnormal);
        rv_dialog_abnormal = findViewById(R.id.rv_dialog_abnormal);
        btn_submit = findViewById(R.id.btn_submit);

        SpannableString ss = new SpannableString("请输入异常描述");//定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14,true);//设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        et_dialog_abnormal.setHint(new SpannedString(ss));

        dialogAbnormalAdapter = new DialogAbnormalAdapter(mList, context);
        rv_dialog_abnormal.setLayoutManager(new GridLayoutManager(context, 3));
        rv_dialog_abnormal.setAdapter(dialogAbnormalAdapter);

        rl_dialog_abnormal_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_dialog_abnormal.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        iv_dialog_abnormal_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnClickListener.onBtnClick(v,mList);
            }
        });

        dialogAbnormalAdapter.setOnClickDialogListener(new DialogAbnormalAdapter.setOnClickItemAdapterListener() {
            @Override
            public void onClickAddPic(int pos) {
               mActivity.RequestLocationAndCallPermission();
            }

            @Override
            public void onClickDeletePic(int pos) {
                deletePic(pos);
            }
        });


    }

    public void addPic(String uri){
        mList.add(uri);
        dialogAbnormalAdapter.notifyDataSetChanged();
    }

    public void deletePic(int pos){
        mList.remove(pos);
        dialogAbnormalAdapter.notifyDataSetChanged();
    }
}
